package com.example.demo.service.impl;


import com.example.demo.mapper.jbgndao;
import com.example.demo.pojo.gonghui;
import com.example.demo.pojo.lishizhanji;
import com.example.demo.pojo.shenqingren;
import com.example.demo.pojo.user;
import com.example.demo.service.sv.jbgn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class jbgnimpl implements jbgn {
   @Autowired
   jbgndao jbgndao;
    @Override
    public user chagerenxinxi(String zhang ){
        return jbgndao.cha(zhang);
    }
   @Override
    public String chazm(String zhang,String mi ){
       String a="";
         if(jbgndao.cha(zhang)==null)
             a="该账号不存在";
         else{
               if(Objects.equals(mi, jbgndao.cha(zhang).getMima()))
                   a="密码正确";
               else
                   a="密码不正确";
         }

        return a ;

    }
    @Override
    public String zhuce(String zhang,String mi){
       String a="";
        if(jbgndao.cha(zhang)!=null)
            a="账号已存在";
        else {
            jbgndao.zeng(zhang, mi);
            a="注册成功";
        }
        return a;
    }

    @Override
    public String gaimima(String zhang, String jiu,String xin){
         String a="修改成功!";
           user user=jbgndao.cha(zhang);
        if(Objects.equals(user.getMima(), jiu))
             jbgndao.gaimi(zhang,xin);
        else
            a="密码错误!";
        return a;
    }
    @Override
    public void chongzhi(String zhang,int qian)
    {    int a=jbgndao.cha(zhang).getJindou()+qian*100;
        jbgndao.gaijindou(zhang,a);
    }

    @Override
    public void delete(String zhang,String mi){
        if(Objects.equals(jbgndao.cha(zhang).getMima(), mi))
               jbgndao.delete(zhang);
    }

    @Override
    public List<user> paihangbang(){
       List<user> ph=jbgndao.paihangbang();
        ph.sort(Comparator.comparing(user::getShengchang).reversed());
       return ph;
    }
    @Override
    public List<gonghui> suoyougonghui(){
        List<gonghui> gh=jbgndao.suoyougonghui();
        gh.sort(Comparator.comparing(gonghui::getRenshu).reversed());
        return gh;
    }
    @Override
    public String jiarugonghui(String zhang,String huizhang){
       String jieguo="申请成功!";
        if(!Objects.equals(jbgndao.cha(zhang).getGonghui(), ""))
            jieguo="你已有公会!";
        else if(jbgndao.chashenqingliebiao(zhang,huizhang)!=null)
            jieguo="你已经申请过加入该公会了!";
        else
             jbgndao.jiarugonghui(zhang,huizhang,jbgndao.cha(zhang).getShengchang());

        return jieguo;
    }
    @Override
    public gonghui wodegonghui(String  zhang){
          if(jbgndao.cha(zhang).getGonghui()==null)
              return null;
          else
        return jbgndao.chagonghui(jbgndao.cha(zhang).getGonghui());

    }
    @Override
    public List<user> gonghuichengyuan(String  zhang){
         List<user> list =jbgndao.chasuoyougonghuichengyuan(jbgndao.cha(zhang).getGonghui());
              list.sort(Comparator.comparing(user::getShengchang).reversed());
        return list;
    }
    @Override
    public List<shenqingren>  shenqinliebiao(String  zhang){
       return jbgndao.shenqinliebiao(zhang);

    }
    @Override
    public void tongyiruhui(String  huizhang,String shenqingren){

        jbgndao.ruhui(shenqingren,jbgndao.cha(huizhang).getGonghui());

    }
    @Override
    public void jujueruhui(String  huizhang,String shenqingren){

        jbgndao.jujueruhui(shenqingren,huizhang);

    }

    @Override
    public String chuangjiangonghui(String gonghuiming, String jianshu, String huizhang) {
            String jieguo="";
            if(jbgndao.chagonghui(gonghuiming)==null)
            {
                if(Objects.equals(jbgndao.cha(huizhang).getGonghui(), ""))
                {   jieguo="创建成功!";
                    jbgndao.chuangjiangonghui(gonghuiming,jianshu,huizhang);
                    jbgndao.ruhui(huizhang,gonghuiming);
                }
                else if(!Objects.equals(jbgndao.cha(huizhang).getGonghui(), ""))
                    jieguo="你已有公会了!";
            }
            else
                jieguo="该公会以存在!";

        return jieguo;
    }
    @Override
    public void jieguo(int jieguo,String zhang,int douzi){
        user user =jbgndao.cha(zhang);
        List<String> listzhanji= new ArrayList<>(List.of(user.getZhanji().split(",")));
        List<String> listdouzi=new ArrayList<>(List.of(user.getJiajiandou().split(",")));
        if(jieguo==1) {
            listzhanji.add(0, "赢");
            listdouzi.add(0, "+" + douzi);
        }
        else if(jieguo==0){
            listzhanji.add(0, "败");
            listdouzi.add(0, "-" + douzi);
        }

          if(listzhanji.size()>20)
              listzhanji.remove(20);
        if(listdouzi.size()>20)
            listdouzi.remove(20);
        int a=user.getJingyan()+douzi/100;
        while(a> user.getDengji()*1000)
         {    a-=user.getDengji()*1000;
             user.setDengji(user.getDengji()+1);
          }
           if(jieguo==1)
           jbgndao.jieguo(String.join(",",listzhanji),String.join(",",listdouzi),zhang,user.getDengji(),a,user.getQuanbuchangci()+1,user.getShengchang()+1 );
             else if(jieguo==0)
               jbgndao.jieguo(String.join(",",listzhanji),String.join(",",listdouzi),zhang,user.getDengji(),a,user.getQuanbuchangci()+1, user.getShengchang() );
    }
    @Override
    public List<lishizhanji> chalishizhanji(String zhang){
        List<lishizhanji> list=new ArrayList<>();
        List<String> listzhanji= List.of(jbgndao.cha(zhang).getZhanji().split(","));
        List<String> listjiajiandou=List.of(jbgndao.cha(zhang).getJiajiandou().split(","));
        for(int a=0;a<listzhanji.size();a++)
        {
            lishizhanji lishizhanji = new lishizhanji();
            lishizhanji.setShengbai(listzhanji.get(a));
            lishizhanji.setJiajiandou(listjiajiandou.get(a));
            list.add(lishizhanji);
        }
        return list;
    }

    @Override
    public void duihuan(String zhang, int xuhao, int jifen) {
        user user=jbgndao.cha(zhang);
        int jifen1 = 0,jindou=0;

         if(xuhao==1)
         {
            jifen1=user.getJifen()-jifen;
               jindou=user.getJindou()+jifen*10;
         }
         else  if(xuhao==2)
         {
             jifen1=0;
             jindou=user.getJindou()+user.getJifen()*10;
         }
         jbgndao.duihuan(zhang,jifen1,jindou);
    }

    @Override
    public void gaijishiqi(String zhang, int jishiqi) {
         jbgndao.gaijishiqi(zhang,jishiqi);
    }

    @Override
    public void gaitouxiangkuang(String zhang, int touxiangkuang) {
        jbgndao.gaitouxiangkuang(zhang,touxiangkuang);
    }

    @Override
    public void gaipaizhuo(String zhang, int paizhuo) {
        jbgndao.gaipaizhuo(zhang,paizhuo);
    }

    @Override
    public void lingjindou(String zhang, int jindou) {
        int a=jbgndao.cha(zhang).getJindou()+jindou;
         jbgndao.lingjindou(zhang,a);
    }
}
